package org.mespilus.mespilusweb.ui.usermanagement.client.model;

import java.util.ArrayList;
import java.util.List;

import org.mespilus.mespilusweb.ui.usermanagement.client.UserManagementService;
import org.mespilus.mespilusweb.ui.usermanagement.client.UserManagementServiceAsync;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.AddMemberEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.AddMemberHandler;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.MemberFoundEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.UserListChangedEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.UserListGetEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.UserListGetHandler;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.MemberListSearchEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.MemberListSearchHandler;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.UserManagementBus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class UserManagementModel implements UserListGetHandler,
		MemberListSearchHandler, AddMemberHandler {
	private final UserManagementServiceAsync userManagementServiceAsync;
	protected List<UsefulMember> persons;
	protected List<UsefulGroup> groups;

	public UserManagementModel() {
		persons = new ArrayList<UsefulMember>();
		groups = new ArrayList<UsefulGroup>();
		userManagementServiceAsync = GWT.create(UserManagementService.class);
		UserManagementBus.getInstance().registerToUserListGet(this);
		UserManagementBus.getInstance().registerToUserListSearch(this);
		UserManagementBus.getInstance().registerToAddUserChange(this);
	}

	public List<UsefulMember> getPersons() {
		return persons;
	}

	public List<UsefulGroup> getGroups() {
		return groups;
	}

	@Override
	public void processGetList(UserListGetEvent event) {
		userManagementServiceAsync
				.getMembers(new AsyncCallback<List<UsefulMember>>() {
					@Override
					public void onFailure(Throwable caught) {
						System.out.println("Error retrieving UserList");
					}

					@Override
					public void onSuccess(List<UsefulMember> result) {
						persons = result;
						UserListChangedEvent event = new UserListChangedEvent();
						event.setMembers(result);
						UserManagementBus.getInstance()
								.fireUserListChangedEvent(event);
					}
				});
		UserListChangedEvent changedEvent = new UserListChangedEvent();
		changedEvent.setMembers(persons);
		UserManagementBus.getInstance().fireUserListChangedEvent(changedEvent);
	}

	@Override
	public void processMemberSelected(final MemberListSearchEvent event) {
		userManagementServiceAsync.getMember(event.getMail(),
				new AsyncCallback<FullMember>() {
					@Override
					public void onSuccess(FullMember result) {
						MemberFoundEvent event = new MemberFoundEvent();
						event.setFullMember(result);
						UserManagementBus.getInstance().fireMemberFoundEvent(
								event);
					}

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("Error recieveng member:"
								+ event.getMail());
					}
				});
	}

	@Override
	public void processAddMember(final AddMemberEvent event) {
		System.out.println("UseRManagementModel.processAddMember");
		userManagementServiceAsync.addMember(event.getMember(),
				new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						System.out.println(UserManagementModel.class
								.getName()
								+ ": Error saving user "
								+ event.getMember().getMail());
					}

					@Override
					public void onSuccess(Void result) {
						UserListGetEvent getListEvent = new UserListGetEvent();
						processGetList(getListEvent);
					}
				});
	}

}
